package com.example.mypigcar.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mypigcar.common.exception.InsufficientBalance;
import com.example.mypigcar.common.exception.RepeatRegisterException;
import com.example.mypigcar.pojo.po.User;
import com.example.mypigcar.pojo.dto.LoginDTO;
import com.example.mypigcar.pojo.dto.UserBalanceDTO;
import com.example.mypigcar.pojo.dto.UserLicenseDTO;
import com.example.mypigcar.mapper.UserDao;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zkw
 * @Description TODO
 * @createTime 2021年07月01日 10:06:00
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public User getUser(LoginDTO user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id","username","name","sex","phone","imageurl","license","balance","type","license_front");
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        User loginUser = userDao.selectOne(queryWrapper);
        return loginUser;
    }

    @Override
    public boolean updateUser(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getUserId());
        return userDao.update(user, queryWrapper) > 0;
    }

    /**
     * 修改用户余额
     * @param user balance正数 执行增加;  balance负数  执行减少
     * @throws InsufficientBalance
     */
    @Override
    public UserBalanceDTO updateUserBalance(User user) throws InsufficientBalance {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User selectUser = userDao.selectOne(queryWrapper);

        //正数 余额++
        if (user.getBalance().compareTo(new BigDecimal(0)) == 1) {
            selectUser.setBalance(selectUser.getBalance().add(user.getBalance()));
        } else {//负数 余额--
            //余额不足
            if (selectUser.getBalance().compareTo(user.getBalance().abs())==-1){
                throw new InsufficientBalance("余额不足");
            }
            selectUser.setBalance(selectUser.getBalance().add(user.getBalance()));
        }
        queryWrapper.eq("username", user.getUsername());
        userDao.update(selectUser, queryWrapper);//更新
        UserBalanceDTO userBalance = new UserBalanceDTO();//方法返回值
        BeanUtils.copyProperties(selectUser, userBalance);
        return userBalance;
    }


    /**
     * 添加用户驾驶证号
     * @param userLicenseDTO 驾驶证对象
     */
    public UserLicenseDTO saveUserLicense(UserLicenseDTO userLicenseDTO){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userLicenseDTO.getUsername());
        User user = new User();
        BeanUtils.copyProperties(userLicenseDTO, user);
        userDao.update(user,queryWrapper);
        return userLicenseDTO;
    }

    /**
     * 查询全部用户
     * @return list
     */
    @Override
    public List<User> listUsers() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id","username","name","sex","phone","imageurl","license","balance","type");
        return userDao.selectList(queryWrapper);
    }

    /**
     * 根据账号查询用户
     * @param username 账号
     * @return 用户
     */
    @Override
    public User getUserByName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_id","username","name","sex","phone","imageurl","license","balance","type");
        queryWrapper.eq("username", username);
        return userDao.selectOne(queryWrapper);
    }

    /**
     * 注册用户
     * @param user 用户对象
     * @return boolean
     * @throws RepeatRegisterException 重复注册异常
     */
    @Override
    public boolean save(User user) throws RepeatRegisterException {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", user.getUsername());
        User user1 = userDao.selectOne(userQueryWrapper);
        if (user1!=null){
            throw new RepeatRegisterException("用户名已存在，请重新输入");
        }
        user.setImageurl("https://pigcar.oss-cn-beijing.aliyuncs.com/user/avatar/2e8508968c8348f2a8e99f3270aeb8ef.jpg");
        return userDao.insert(user)>0;
    }

    /**
     * 添加用户的驾驶证正面和背面
     * @param user 驾驶证正面和背面url
     * @return boolean
     */
    @Override
    public boolean insertUserLicenseImg(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getUserId());
        int update = userDao.update(user, queryWrapper);
        return update>0;
    }

    @Override
    public boolean delete(Integer userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return userDao.delete(queryWrapper)>0;
    }

    /**
     * 根据手机号查找用户
     * @param phone 手机号
     * @return boolean
     */
    @Override
    public boolean getUserByPhone(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        User user = userDao.selectOne(queryWrapper);
        return user == null;
    }

}
